package com.safeluck.floatwindow.usbVideo;

/**
 * @ProjectName: aaa
 * @Package: com.safeluck.floatwindow.usbVideo
 * @ClassName: WaterMakeUtils
 * @Description: java类作用描述
 * @Author: zhanwei.li
 * @CreateDate: 2021/8/5 16:08
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/8/5 16:08
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WaterMakeUtils {
    private static final String TAG ="WaterMakeUtils";
    private static Bitmap Jpg2Bitmap(byte []jpg) {
        return BitmapFactory.decodeByteArray(jpg, 0, jpg.length);
    }
    public static Bitmap convert90(Bitmap a)
    {
        int w = a.getWidth();
        int h = a.getHeight();
        Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(newb);
        Matrix m = new Matrix();
//        m.postScale(1, -1);   //镜像垂直翻转
//        m.postScale(-1, 1);   //镜像水平翻转
        m.postRotate(-90);  //旋转-90度
        Bitmap new2 = Bitmap.createBitmap(a, 0, 0, w, h, m, true);
        cv.drawBitmap(new2, new Rect(0, 0, new2.getWidth(), new2.getHeight()),new Rect(0, 0, w, h), null);
        return newb;
    }
    public static Bitmap AddWaterMake(Context context, int type, Bitmap src, int resolution, String school, String teacher,
                                      String student, double speed, String car_id, double latitude, double longitude, Date time) {
        int fontSize = 11;

        Bitmap bitmap = src;

        Log.i(TAG,"resolution="+resolution);

        if (resolution == 320) {
            fontSize = 10;
        } else if (resolution == 640) {
            fontSize = 20;
        } else {
            fontSize = 40;
        }

        if (type == 1) {
            bitmap = ImageUtil.drawTextToLeftBottom(context, bitmap, "驾校：" + school, fontSize, Color.GREEN, 0, fontSize*2 + 2);
            bitmap = ImageUtil.drawTextToLeftBottom(context, bitmap, String.format("%.1f km/h", speed), fontSize, Color.GREEN, 0, fontSize + 2);
            bitmap = ImageUtil.drawTextToRightBottom(context, bitmap, String.format("%.6f %.6f", latitude, longitude), fontSize, Color.GREEN, 0, fontSize + 2);
            bitmap = ImageUtil.drawTextToLeftBottom(context, bitmap, "教练：" + teacher, fontSize, Color.GREEN, 0, 2);
            bitmap = ImageUtil.drawTextToRightBottom(context, bitmap, "学员：" + student, fontSize, Color.GREEN, 0, 2);
        } else {
            bitmap = ImageUtil.drawTextToLeftTop(context, bitmap, "驾校：" + school, fontSize, Color.GREEN, 0, 0);
            bitmap = ImageUtil.drawTextToLeftTop(context, bitmap, "教练：" + teacher, fontSize, Color.GREEN, 0, fontSize);
            bitmap = ImageUtil.drawTextToLeftTop(context, bitmap, "学员：" + student, fontSize, Color.GREEN, 0, fontSize * 2);
            bitmap = ImageUtil.drawTextToLeftBottom(context, bitmap, String.format("%.1f km/h", speed), fontSize, Color.GREEN, 0, fontSize + 2);
            bitmap = ImageUtil.drawTextToLeftBottom(context, bitmap, car_id, fontSize, Color.GREEN, 0, 2);
            bitmap = ImageUtil.drawTextToRightBottom(context, bitmap, String.format("%.6f %.6f", latitude, longitude), fontSize, Color.GREEN, 0, fontSize + 2);
            bitmap = ImageUtil.drawTextToRightBottom(context, bitmap, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time), fontSize, Color.GREEN, 0, 2);
        }

        int quality = 100;
        if (resolution == 320){
            quality = 95;
        } else if (resolution == 640) {
            quality = 75;
        } else {
            quality = 90;
        }



        return bitmap;
    }

    private static Bitmap changeBitmapSize(Bitmap bitmap_, int reqWidth, int reqHeight) {

        int width = bitmap_.getWidth();
        int height = bitmap_.getHeight();

//设置想要的大小
        int newWidth=reqWidth;
        int newHeight=reqHeight;

//计算压缩的比率
        float scaleWidth=((float)newWidth)/width;
        float scaleHeight=((float)newHeight)/height;

//获取想要缩放的matrix
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth,scaleHeight);

//获取新的bitmap
        bitmap_= Bitmap.createBitmap(bitmap_,0,0,width,height,matrix,true);
        bitmap_.getWidth();
        bitmap_.getHeight();

        return bitmap_;
    }

    public static Bitmap convert0(Bitmap a) {
        int w = a.getWidth();
        int h = a.getHeight();
        Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
        Canvas cv = new Canvas(newb);
        Matrix m = new Matrix();
//        m.postScale(1, -1);   //镜像垂直翻转
//        m.postScale(-1, 1);   //镜像水平翻转
        m.postRotate(90);  //旋转-90度
        Bitmap new2 = Bitmap.createBitmap(a, 0, 0, w, h, m, true);
        cv.drawBitmap(new2, new Rect(0, 0, new2.getWidth(), new2.getHeight()),new Rect(0, 0, w, h), null);
        return newb;
    }
}
